﻿using Coldairarrow.Entity.Base_leasesystem_service;
using Coldairarrow.Entity.Base_leasesystem_serviceaccount;
using Coldairarrow.Entity.Base_leasesystem_servicebank;
using Coldairarrow.Entity.ViewModel;
using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_service
{
    public class leasesystem_serviceBusiness : BaseBusiness<leasesystem_service>, Ileasesystem_serviceBusiness, ITransientDependency
    {
        public leasesystem_serviceBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<leasesystem_service>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_service>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_service, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<leasesystem_service> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_service data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_service data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }
        public object GetService_Select()
        {
            var query = Db.GetIQueryable<leasesystem_service>().Select(a => new
            {
                a.Id,
                a.ServiceName
            });
            return query;
        }

        public object SettlementExcel(DataTable dt) 
        {
            List<DataTableHelp> list = new List<DataTableHelp>();
            bool istrue = true;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString().IsNullOrEmpty())
                {
                    DataTableHelp help = ReadSettlementHelp(dt, i, "姓名", true, "");
                    list.Add(help);
                    istrue = false;
                    continue;
                }
                else if (dt.Rows[i][1].ToString().IsNullOrEmpty())
                {
                    DataTableHelp help1 = ReadSettlementHelp(dt, i, "身份证", true, "");
                    list.Add(help1);
                    istrue = false;
                    continue;
                }
                else if (dt.Rows[i][2].ToString().IsNullOrEmpty())
                {
                    DataTableHelp help1 = ReadSettlementHelp(dt, i, "联系电话", true, "");
                    list.Add(help1);
                    istrue = false;
                    continue;
                }
                else if (!UtilHelper.IsIdCardNo(dt.Rows[i][1].ToString()))
                {
                    DataTableHelp help1 = ReadSettlementHelp(dt, i, "", false, "身份证格式不正确");
                    list.Add(help1);
                    istrue = false;
                    continue;
                }
                else if (!UtilHelper.IsPhone(dt.Rows[i][2].ToString()))
                {
                    DataTableHelp help1 = ReadSettlementHelp(dt, i, "", false, "手机号格式不正确");
                    list.Add(help1);
                    istrue = false;
                    continue;
                }
                else
                {
                    DataTableHelp help1 = ReadSettlementHelp(dt, i, "", false, "数据完整可导入");
                    list.Add(help1);
                }
            }
            var result = new
            {
                Total = list.Count(),
                Data = list,
                istrue = istrue
            };
            return result;

        }


        public DataTableHelp ReadSettlementHelp(DataTable dt, int index, string note, bool istrue, string other)
        {
            DataTableHelp help = new DataTableHelp();

            help.Name = dt.Rows[index][0].ToString();
            help.IdNumber = dt.Rows[index][1].ToString();
            help.Phone = dt.Rows[index][2].ToString();
            help.BankAcc = dt.Rows[index][3].ToString();
            help.BankName = dt.Rows[index][4].ToString();
            if (!dt.Rows[index][5].ToString().IsNullOrEmpty())
            {
                help.Rental = decimal.Parse(dt.Rows[index][5].ToString());
            }

            help.Remark = dt.Rows[index][6].ToString();
            if (istrue)
            {
                help.result = note + "信息未填写";
            }
            else
            {
                help.result = other;
            }

            return help;
        }


        #endregion

        #region 私有成员

        #endregion
    }
}